#!/bin/bash
#auto backup mysql
#authors cq 2024
backup_dir=/data/mysql/t1-`date +%Y-%m-%d`/test_db
MYSQLCMD=mysqldump
MYSQLPASSWD=123456
MYDATABASE=test_db
MYFILE=test

#先判断是否为root用户
if [ ! $UID -eq 0 ];then
	echo -e "\033[32m you are not root,you can not backup \033[0m"
	exit 1
fi


#再判断目录是否存在

if [-d $backup_dir ];then
	echo -e "\033[32m $backup_dir is exits \033[0m"
else
	mkdir -pv $backup_dir
	echo -e "\033[32m The $backup_dir create sucessful\033[0m"

fi

#调用mysqldump语句来进行备份
$MYSQLCMD -ubackup -p$MYSQLPASSWD  -B $MYDATABASE > $backup_dir/$MYFILE.sql

#判断上诉指令是否被执行

if [  $? -eq 0 ]; then
	echo -e "\033[32m The mysqldump is sucess"
else 
	echo -e "\033[32m the mysqldump is failed"

fi
